package com.sf.dlb.controller;

import com.sf.dlb.bean.VideoRquestBean;
import com.sf.dlb.constant.ResponseConstant;
import com.sf.dlb.jpa.model.TokenEntity;
import com.sf.dlb.service.UserInfoService;
import com.sf.dlb.service.VideoListService;
import com.sf.dlb.service.VisitorInfoService;
import com.sf.dlb.service.VisitorRecordService;
import com.sf.dlb.utils.GsonUtils;
import com.sf.dlb.utils.ResultVOUtil;
import com.sf.dlb.vo.ResultVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 视频
 *
 * Created by tanjing on 2018/12/19.
 */
@RestController
@RequestMapping(value = "/v1/video")
public class VideoController extends BaseController{

    @Autowired
    private VideoListService videoListService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private VisitorRecordService visitorRecordService;
    @Autowired
    private VisitorInfoService visitorInfoService;
    /**
     * 获取视频列表
     * @param request
     * @param videoRquestBean
     * @return
     */
    @PostMapping(value = "/list")
    @ResponseBody
    public Object list(HttpServletRequest request,@RequestBody VideoRquestBean videoRquestBean){
        String token=request.getHeader(global.token_request_header_key);
        ResultVO resultVO;
        logger.info("list request json === " + GsonUtils.getInstance().toJson(videoRquestBean));
        if(videoRquestBean.getCurrentPages()!=0){
            videoRquestBean.setCurrentPages(videoRquestBean.getCurrentPages()-1);//从0开始算；
        }
        if(videoRquestBean.getPageSize()==0){
            videoRquestBean.setPageSize(20);//默认20条
        }
        if(StringUtils.isBlank(token)||StringUtils.contains(token,global.user_visitor_token_prefix)){
            resultVO=videoListService.findbyListNotLogin(videoRquestBean);
        }else{
            resultVO=userInfoService.getTokenChekout(request);
            if(resultVO.getCode().intValue()==200){
                TokenEntity tokenEntity = (TokenEntity) resultVO.getData();
                videoRquestBean.setUserId(tokenEntity.getUserId());
                resultVO = videoListService.findbyList(videoRquestBean);
            }
        }
        logger.info("list response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 视频播放/结束
     * @param request
     * @return
     */
    @PostMapping(value = "/play")
    @ResponseBody
    public Object play(HttpServletRequest request,@RequestBody  VideoRquestBean videoRquestBean){
//        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        String token=request.getHeader(global.token_request_header_key);
        logger.info("play request json === " + GsonUtils.getInstance().toJson(videoRquestBean));
        if(StringUtils.isEmpty(videoRquestBean.getVideoId())){
            return ResultVOUtil.error(ResponseConstant.ERROR,"视频id不能为空");
        }
        if(StringUtils.isEmpty(videoRquestBean.getStartTime())&&StringUtils.isEmpty(videoRquestBean.getEndTime())){
            return ResultVOUtil.error(ResponseConstant.ERROR,"视频时间不能为空");
        }
        if(StringUtils.isNotBlank(videoRquestBean.getStartTime())&&StringUtils.isNotBlank(videoRquestBean.getEndTime())){
            return ResultVOUtil.error(ResponseConstant.ERROR,"开始时间与结束时间不能同时传输");
        }
        ResultVO resultVO;
        if(StringUtils.isBlank(token)||StringUtils.contains(token,global.user_visitor_token_prefix)) {
            resultVO=visitorInfoService.getTokenChekout(request);
            if(resultVO.getCode().intValue()!=200) {
                return resultVO;
            }
            TokenEntity tokenEntity =(TokenEntity)resultVO.getData();
            videoRquestBean.setUserId(tokenEntity.getUserId());
            if(StringUtils.isNotBlank(videoRquestBean.getStartTime())){
                resultVO = visitorRecordService.startPlay(videoRquestBean);
            }else if(StringUtils.isNotBlank(videoRquestBean.getEndTime())){
                resultVO = visitorRecordService.endPlay(videoRquestBean);
            }
        }else {
            resultVO=userInfoService.getTokenChekout(request);
            if(resultVO.getCode().intValue()!=200) {
                return resultVO;
            }
            TokenEntity tokenEntity = (TokenEntity) resultVO.getData();
            videoRquestBean.setUserId(tokenEntity.getUserId());
            if (StringUtils.isNotBlank(videoRquestBean.getStartTime())) {
                resultVO = videoListService.startPlay(videoRquestBean);
            } else if (StringUtils.isNotBlank(videoRquestBean.getEndTime())) {
                resultVO = videoListService.endPlay(videoRquestBean);
            }
        }
        logger.info("play response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
}
